import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    children: [
      {
        path: '/users',
        name: 'users',
        component:()=>import ('../views/users/users.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component:()=>import ('../views/authority/rights.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component:()=>import ('../views/authority/roles.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/shop/goods.vue')
      },
      {
        path: '/addgoods',
        name: 'addgoods',
        component: () => import ('../views/shop/addGoods.vue')
      },
      {
        path: '/params',
        name: 'params',
        component:()=>import ('../views/shop/params.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        component:()=>import ('../views/shop/categories.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component:()=>import ('../views/orders/orders.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component:()=>import ('../views/reports/reports.vue')
      },
    ]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component:()=>import ('../views/login/login.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import ('../views/index.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import ('../views/user.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  let inlogin = localStorage.getItem('token')
  if (!inlogin) {
    if (to.name == 'login') {
      next()
    } else {
      next('/login')
    }
  } else {
    if (to.name == 'login') {
      next('/')
    } else {
      next()
    }
  }
})

export default router
